﻿using System;
using System.Collections;
using UnityEngine;

namespace Asset
{
    public class AsyncSceneBundle : AsyncBundle
    {
        private Action<int> progressHandler;
        public Action<int> ProgressHandler
        {
            get
            {
                return this.progressHandler;
            }
            set
            {
                this.progressHandler = value;
                if (this.Status == AsyncStatus.Finish)
                {
                    if (progressHandler != null)
                        progressHandler(100);
                }
            }
        }

        public AsyncSceneBundle(string url) : base(url)
        {

        }

        public override IEnumerator doLoad()
        {
            if (this.Status == AsyncStatus.Waiting)
            {
                this.asyncStatus = AsyncStatus.Running;
                request = AssetBundle.LoadFromFileAsync(this.resPath);
                yield return request;
            }
            else if (this.Status == AsyncStatus.Running)
            {
                if (this.request.isDone)
                {
                    this.asyncStatus = AsyncStatus.Finish;
                    this.assetBundle = request.assetBundle;
                }
                if (progressHandler != null)
                    progressHandler(this.Status == AsyncStatus.Finish ? 100 : (int)(this.request.progress * 100));
            }
        }

    }
}
